package com.ourchem.web.controller.bus;

import java.util.List;
import java.util.Map;

import com.ourchem.bus.domain.IpView;
import com.ourchem.bus.service.IIpViewService;
import com.ourchem.common.utils.DateUtils;
import com.ourchem.web.controller.dto.DayTimeVo;
import com.sun.jna.platform.unix.solaris.LibKstat;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.ourchem.common.annotation.Log;
import com.ourchem.common.core.controller.BaseController;
import com.ourchem.common.core.domain.AjaxResult;
import com.ourchem.common.enums.BusinessType;

import com.ourchem.common.utils.poi.ExcelUtil;
import com.ourchem.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户访问量Controller
 *
 * @author ourchem
 * @date 2021-11-02
 */
@RestController
@RequestMapping("/bus/ipView")
public class IpViewController extends BaseController
{
    @Autowired
    private IIpViewService ipViewService;


    /**
     * 查询用户访问量列表
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:list')")
    @GetMapping("/list")
    public TableDataInfo list(IpView ipView)
    {
        startPage();
        List<IpView> list = ipViewService.selectIpViewList(ipView);
        return getDataTable(list);
    }

    /**
     * 导出用户访问量列表
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:export')")
    @Log(title = "用户访问量", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IpView ipView)
    {
        List<IpView> list = ipViewService.selectIpViewList(ipView);
        ExcelUtil<IpView> util = new ExcelUtil<IpView>(IpView.class);
        return util.exportExcel(list, "用户访问量数据");
    }

    /**
     * 获取用户访问量详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(ipViewService.selectIpViewById(id));
    }

    /**
     * 修改用户访问量
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:edit')")
    @Log(title = "用户访问量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IpView ipView)
    {
        return toAjax(ipViewService.updateIpView(ipView));
    }

    /**
     * 删除用户访问量
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:remove')")
    @Log(title = "用户访问量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(ipViewService.deleteIpViewByIds(ids));
    }



    /**
     * 获取用户ip ，判断是否登录存入数据库,不用判断权限
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:isView')")
    @GetMapping("/is_view/{ipAddress}")
    public AjaxResult isView(@PathVariable String ipAddress)
    {
        ipViewService.insertIpView(ipAddress);
        return AjaxResult.success("成功");
    }

    /**
     * 获取用户日访问量
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:day_count')")
    @GetMapping("/day_count/{day}")
    public AjaxResult dayCount(@PathVariable String day){
        int i = ipViewService.selectIpViewByOneDayCount(day);
        return AjaxResult.success("查询成功",i);
    }

    /**
     * 获取月访问量和全部数量
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('bus:ipView:months_count')")
    @PostMapping("/months_count")
    public AjaxResult monthsCount(@RequestBody(required = false) DayTimeVo dayTimeVo){
        if(dayTimeVo==null){
            dayTimeVo = new DayTimeVo();
            dayTimeVo.setDay1("");
            dayTimeVo.setDay2("");
        }
        if(dayTimeVo!=null && dayTimeVo.getDay1()==null){
            dayTimeVo.setDay1("");
        }
        if(dayTimeVo!=null && dayTimeVo.getDay2()==null){
            dayTimeVo.setDay2("");
        }
        Map map = ipViewService.selectIpViewByAllMonthsCount(dayTimeVo.getDay1(),dayTimeVo.getDay2());
        return AjaxResult.success("查询成功",map);
    }


//
//    @PreAuthorize("@ss.hasPermi('bus:ipView:days_count')")
//    @RequestMapping(value = {"/days_count/{day1}/{day2}","/days_count"},method = {RequestMethod.GET,RequestMethod.GET})
//    public AjaxResult daysCount(@PathVariable(value = "day1",required = false) String day1, @PathVariable(value = "day2",required = false)String day2){
//        Map map = ipViewService.selectIpViewByAllDaysCount(day1, day2);
//        return AjaxResult.success("查询成功",map);
//    }


}
